import React, { useState,useEffect } from 'react'
import { Input, Space, Button } from 'antd';
import { useHistory } from 'react-router-dom';
import _ from 'lodash'
import { fetchData } from '../request'
import './index.scss'

const Login = (props) => {
    let history = useHistory()

    const [userName, setUserName] = useState('')
    const [password, setPassword] = useState('')
    const [url, setUrl] = useState({})

    const onChangeUserName = e => {
        setUserName(e.target.value)
    }
    const onChangePassword = e => {
        setPassword(e.target.value)
    }

    const onLogin = () => {
        setUrl({
            method: "post",
            url: "/makeUp/account/login",
            data: {
                userName: userName,
                password: password
            }
        })
    }

    useEffect(() => {
        console.log(localStorage.getItem("parentId"));
        if(localStorage.getItem("parentId") == null){
            fetchData(url).then(data => {
                console.log("44455566677555", data);
                localStorage.setItem("id", data.data.id)
                localStorage.setItem("userName", data.data.userName)
                localStorage.setItem("role", data.data.role)
                localStorage.setItem("parentId", data.data.parentId)

                history.push("/person")
            })
        }else{
            history.push("/person")
        } 
    }, [url])


    return (
        <div className="login-page">
            <div className="bg"></div>
            <div className="login-box">
                <div className="name" style={{fontSize: 24, marginBottom: 30}}>万都国际化妆品有限公司</div>
                <Input size="large" placeholder="userName" onChange = {onChangeUserName} />
                <Space direction="vertical">
                    <Input.Password placeholder="password" onChange = {onChangePassword} />
                </Space>
                <Button type="primary" onClick={onLogin}>登陆</Button>
            </div>
            
        </div>
    )
}

export default Login;